package com.lin.controller;

import com.lin.pojo.Books;
import com.lin.service.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.lang.Exception;
@Data
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list = bookService.selectAll();
        model.addAttribute("list",list);
        return "allBook";
    }
    @RequestMapping("/toaddPage")
    public String toaddBook(){
        return "addBook";
    }
     @RequestMapping("/addBook")
    public String addPage(Books books){
        System.out.println(books);
        bookService.insertBook(books);
        return "redirect:/book/allBook";
    }
     @RequestMapping("/toupdatePage")
    public String toupdateBook(Model model,int id){
        Books books = bookService.selectById(id);
        model.addAttribute("book",books);
        return "updateBook";
    }
    @RequestMapping("/updateBook")
    public String updatePage(Model model,Books book){
         bookService.updateBook(book);
         Books books = bookService.selectById(book.getBookID());
         model.addAttribute("books",books);
         return "redirect:/book/allBook";
    }
    @RequestMapping("/del/{bookid}")
    public String deleteBook(@PathVariable("bookid") int id){
       bookService.deleteBook(id);
       return "redirect:/book/allBook";
    }
    @RequestMapping("/jumplogin")
    public String jumpLogin() throws Exception{
        return "login";
    }
    //跳转到成功页面
    @RequestMapping("/jumpsucess")
    public String jumpSucess() throws Exception{
        return "redirect:/book/allBook";
    }
    //登录提交
    @RequestMapping("/login")
    public String login(HttpSession session, String username, Model model) throws Exception{
        System.out.println("接收前端===="+username);
        session.setAttribute("user",username);
        model.addAttribute("user",username);
        return "redirect:/book/allBook";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session) throws Exception{
        //session过期
        session.invalidate();
        return "redirect:/book/allBook";
    }
    @RequestMapping("/selectBookByName")
    public String selectBookByName(String bookName,Model model){
        System.out.println("selectBookByName==>"+bookName);
        if(bookName != null && bookName.equals("")){
            List<Books> list = bookService.selectBookByName(bookName);
            model.addAttribute("list",list);
            return "allBook";
        }
        model.addAttribute("error","未查询到书籍，请重新输入");
        System.err.println("error");
        return "allBook";
    }
}
